﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel;
using ECCSystem.BizRule;
using System.IO;
namespace ECCSystem.Reports
{
	public partial class frmDetailMemberReport : Form
	{
		public frmDetailMemberReport()
		{
			InitializeComponent();
		}
		DataSet objDS = new DataSet();
		BizRule.ClsReports objBRReports;
		BizRule.ClsCommon objCommon;
		//BizRule.ClsUserMaintenance objBROperator;

		private void frmPersonalMailReport_Load(object sender, EventArgs e)
		{
			ReadXML.ReadXml(this);
			dtpFrom.Text = DateTime.Now.ToShortDateString();
			dtpTo.Text = DateTime.Now.ToShortDateString();

			ClsTeamGoal objClsTeamGoal = new ClsTeamGoal();
			DataSet objDSTmp = (DataSet)objClsTeamGoal.GetList(dtpFrom.Value);
			if (objDSTmp.Tables[0].Rows.Count > 0)
			{
				txtEfficiency.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_Efficiency"].ToString();
				txtFactAchievement.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_FactAchievement"].ToString();
				txtDefect.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_Defect"].ToString();
				txtDefectQuotietyrash.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_DefectQuotietyrash"].ToString();
			}
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			if (this.CheckDate() == false)
			{
				MessageBox.Show("終了日付は開始日付より遅い。", "ECC Information");
				return;
			}
			btnCreate.Enabled = false;

			ClsTeamGoal objClsTeamGoal = new ClsTeamGoal();
			DataSet objDSTmp = (DataSet)objClsTeamGoal.GetList(dtpFrom.Value);
			if (objDSTmp.Tables[0].Rows.Count > 0)
			{
				txtEfficiency.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_Efficiency"].ToString();
				txtFactAchievement.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_FactAchievement"].ToString();
				txtDefect.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_Defect"].ToString();
				txtDefectQuotietyrash.Text = objDSTmp.Tables[0].Rows[0]["TeamGoal_DefectQuotietyrash"].ToString();
			}
			else
			{
				txtEfficiency.Text = "";
				txtFactAchievement.Text = "";
				txtDefect.Text = "";
				txtDefectQuotietyrash.Text = "";
			}

			//open data source
			objBRReports = new ECCSystem.BizRule.ClsReports();
			objDS = (DataSet)objBRReports.GetDetailMemberReport(dtpFrom.Value, dtpTo.Value);
			if (objDS.Tables[0].Rows.Count <= 0)
			{
				MessageBox.Show("データ無し。", "ECC Information");
				btnCreate.Enabled = true;
				dgvShow.DataSource = null;
				return;
			}
			else
			{
				dgvShow.DataSource = null;
				dgvShow.AutoGenerateColumns = false;
				dgvShow.DataSource = objDS.Tables[0];
			}

			////create report
			//this.CreateExcel();

			btnCreate.Enabled = true;
		}

		//Check start date less than end date
		private bool CheckDate()
		{
			if (dtpFrom.Value > dtpTo.Value)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		//excel operation(open,fill,quit)
		private void CreateExcel()
		{
			Excel.ApplicationClass objExlApp = new Excel.ApplicationClass();
			Excel.Workbook theWookbook = null;
			Excel.Worksheet theWookSheet = null;
			objCommon = new ECCSystem.BizRule.ClsCommon();
			try
			{
			    objExlApp.Visible = false;

			    object objMissing = System.Reflection.Missing.Value;
			    StringBuilder strSavePath = new StringBuilder();
			    strSavePath.Append("C:\\ECCReport\\");
			    //if strSavePath is not existing,create the path
			    if (Directory.Exists(strSavePath.ToString()) == false)
			    {
			        Directory.CreateDirectory(strSavePath.ToString());
			    }

			    StringBuilder strTemplatePath = new StringBuilder();
			    //Excel template path
			    strTemplatePath.Append(System.Windows.Forms.Application.StartupPath);
			    strTemplatePath.Append("\\PersonalMailReport.xls");
			    //if this template is existing or not
			    if (File.Exists(strTemplatePath.ToString()) == false)
			    {
			        MessageBox.Show("レポートのテンプレートファイルが見つかりません。", "ECC Information");
			        //quit excel object
			        objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
			        System.GC.Collect();
			        btnCreate.Enabled = true;
			        return;
			    }
			    //open the excel template
                theWookbook = objExlApp.Workbooks.Open(strTemplatePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                                         objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
			    //get the first sheet
			    theWookSheet = (Excel.Worksheet)theWookbook.Worksheets[1];

			    //fill data into excel
			    theWookSheet.Cells[2, 1] = dtpFrom.Value.Date.ToString("yyyy/MM/dd") + "-" + dtpTo.Value.Date.ToString("yyyy/MM/dd");
			    for (int i = 0; i < objDS.Tables[0].Rows.Count; i++)
			    {
					theWookSheet.Cells[i + 5, "A"] = objDS.Tables[0].Rows[i]["LogonName"].ToString();
					theWookSheet.Cells[i + 5, "B"] = objDS.Tables[0].Rows[i]["UserName"].ToString();
					theWookSheet.Cells[i + 5, "C"] = objDS.Tables[0].Rows[i]["Category_Name"].ToString();
					theWookSheet.Cells[i + 5, "D"] = objDS.Tables[0].Rows[i]["CategoryContent_Name"].ToString();
					int intA = Convert.ToInt32(objDS.Tables[0].Rows[i]["a"].ToString());
					int intB = Convert.ToInt32(objDS.Tables[0].Rows[i]["b"].ToString());
					int intC = Convert.ToInt32(objDS.Tables[0].Rows[i]["c"].ToString());
					int intD = Convert.ToInt32(objDS.Tables[0].Rows[i]["d"].ToString());
					theWookSheet.Cells[i + 5, "E"] = intA + intB + intC + intD;
					theWookSheet.Cells[i + 5, "F"] = intA;
					theWookSheet.Cells[i + 5, "G"] = intB;
					theWookSheet.Cells[i + 5, "H"] = intC;
					theWookSheet.Cells[i + 5, "I"] = intD;

			    }

			    //set excel report save path and name
				strSavePath.Append("PersonalMailReport" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString());
			    strSavePath.Append(DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".xls");

			    //if the report has existed or not
			    if (File.Exists(strSavePath.ToString()) == true)
			    {
			        MessageBox.Show("このファイルがもう存在しました！", "ECC Information");
			        //quit excel object
			        objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
			        System.GC.Collect();
			        btnCreate.Enabled = true;
			        return;
			    }

			    //save the report
                theWookbook.SaveAs(strSavePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                    Excel.XlSaveAsAccessMode.xlNoChange, objMissing, objMissing, objMissing, objMissing);
			    //prompt save success
			    MessageBox.Show("操作成功しました。", "ECC Information");

			    //quit excel object
			    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
			    System.GC.Collect();
			    this.Close();
			}
			catch (Exception ex)
			{
			    //quit excel object
			    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
			    System.GC.Collect();

			    //MessageBox.Show(ex.ToString(), "ECC Catch Error");
			    btnCreate.Enabled = true;
			}

		}

		private void frmDetailMemberReport_FormClosing(object sender, FormClosingEventArgs e)
		{
			ReadXML.SaveXml(this);
		}
	}
}